package com.health.life;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.health.life.enty.CookClass;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends MyBaseActivity implements ExpandableListView.OnGroupExpandListener, ExpandableListView.OnChildClickListener {
    private ExpandableListView exListView;
    private boolean isSub=false;
    private  int gid=0;
    private DbUtils dbUtils;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dbUtils=DbUtils.create(this,"cook.db");
        try {
            List<CookClass> temList=dbUtils.findAll(Selector.from(CookClass.class).where("cookclass","=","0"));
            if (temList!=null&&temList.size()>0)
                listParent=temList;
        } catch (DbException e) {
            e.printStackTrace();
        }
        exListView = (ExpandableListView) findViewById(R.id.exlistView);
        exListView.setAdapter(mAdapter);
        exListView.setOnGroupExpandListener(this);
        exListView.setOnChildClickListener(this);
        exListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                if (exListView.isGroupExpanded(groupPosition))
                {
                    exListView.collapseGroup(groupPosition);
                    return false;
                }
                isSub=true;
                gid=groupPosition;
                getData(listParent.get(groupPosition).getId());

                return false;
            }
        });
    }

    public void getData(String id){

        try {
            List<CookClass> temList=dbUtils.findAll(Selector.from(CookClass.class).where("cookclass","=",id));
            if (temList!=null&&temList.size()>0)
            {
                listchild.clear();
                listchild.addAll(temList);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        if (listchild!=null&&listchild.size()>0)
        {
            mAdapter.notifyDataSetChanged();
        }
    }




    private List<CookClass> listParent=new ArrayList<>(0);
    private List<CookClass> listchild=new ArrayList<>(0);
    BaseExpandableListAdapter mAdapter=new BaseExpandableListAdapter() {
        @Override
        public int getGroupCount() {
            return listParent.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return gid==groupPosition?listchild.size():0;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return listParent.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return listchild.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView= (TextView) LayoutInflater.from(MainActivity.this).inflate(android.R.layout.simple_list_item_1,null,false);
            textView.setText(listParent.get(groupPosition).getName());
            return textView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            TextView textView= (TextView) LayoutInflater.from(MainActivity.this).inflate(android.R.layout.simple_list_item_1,null,false);
            textView.setText(listchild.get(childPosition).getName());
            return textView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    };

    @Override
    public void onGroupExpand(int groupPosition) {
        for (int i = 0; i < mAdapter.getGroupCount(); i++) {
            if (groupPosition != i) {
                exListView.collapseGroup(i);
            }
        }
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Intent intent=new Intent(this,CookListActivity.class);
        intent.putExtra("ID",listchild.get(childPosition).getId());
        startActivity(intent);
        return false;
    }
}
